; petscii.asm
; Pet ASCII related routines

	.export pet2scr
	.export scr2pet
	
	.segment "DATA"
	
sccode:	.res 1
	
	.segment "CODE"
	
pet2scr:	; A .. petscii, -> A .. screen code
			; $00..$1F   ->   $80 .. $9F (not printable)
			; $20..$3F   ->   $20 .. $3F
			; $40..$5F   ->   $00 .. $1F
			; $60..$7F   ->   $40 .. $5F
			; $80..$9F   ->   $A0 .. $BF (not printable)
			; $A0..$BF   ->   $60 .. $7F
			; $C0..$FE   ->   $40 .. $7E
			; $FF        ->   $5E        (pi)
			asl
			bcs @GT80
			lsr
			cmp #$20
			bcc @control1
			cmp #$60
			bcc @LT60
			and #$DF
			bne @fin1
@LT60:		and #$3F
@fin1:		rts
@GT80:		bmi @GTC0
			lsr
			cmp #$20
			bcc @control2
			ora #$40
			rts
@control1:	ora #$80
			rts
@GTC0:		lsr
			cmp #$7F
			bne @nopi
			lda #$5E
@nopi:		rts
@control2:	ora #$A0
			rts

scr2pet:	; A .. screen code, -> A .. petscii
			; TODO:
			; $00..$1F ->   $40 .. $5F
			; $20..$3F ->   $20 .. $3F
			; $40..$5F ->   $60 .. $7F   
			; $60..$7F ->   $A0 .. $BF   
			; $80..$9F ->   $00 .. $1F (not printable)
			; $A0..$BF ->   $80 .. $9F (not printable)
			sta sccode
			and #$3F
			asl sccode	; save bit 7
			bit sccode	; check bit 6
			bpl @skpor1
			ora #$80
@skpor1:	bvs @skpor2
			ora #$40
@skpor2:	rts
